package vincent.com.layouttest.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by chenzhengwen on 2015/6/17.
 */
public class BrokenLineChartView extends View {
    private final float DEFAULT_SIZE = 300;
    private float[] mPointers;

    public BrokenLineChartView(Context context) {
        this(context, null);
    }

    public BrokenLineChartView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BrokenLineChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setPointers(float[] pointers){
        this.mPointers = pointers;
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int size = (int)(getContext().getResources().getDisplayMetrics().density * DEFAULT_SIZE);
        setMeasuredDimension(size, size);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawARGB(255, 24, 142, 190);

        Paint linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setColor(Color.WHITE);

        float lastX = -1;
        float lastY = -1;

        Path path = new Path();

        if(mPointers != null && mPointers.length > 0){
            for(int i = 0; i < mPointers.length; i+=2){
                float x = mPointers[i];
                float y = mPointers[i+1];

                if(lastX > 0 || lastY > 0){
                    canvas.drawLine(lastX, lastY, x, y, linePaint);
                }

                lastX = x;
                lastY = y;
            }
        }
    }
}
